package org.linlinjava.litemall.db.service;

import org.linlinjava.litemall.db.dao.LitemallActivityPicMapper;
import org.linlinjava.litemall.db.domain.LitemallActivityPic;
import org.linlinjava.litemall.db.domain.LitemallActivityPicExample;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallActivityPicService {

    @Resource
    private LitemallActivityPicMapper litemallActivityPicMapper;

    /**
     * 添加团购活动用户信息
     *
     * @param entity
     */
    public void addActivityPic(LitemallActivityPic entity, LitemallAdmin admin) {
        entity.setUpdateBy(admin != null ? admin.getId().toString() : "0");
        entity.setAddBy(admin != null ? admin.getId().toString() : "0");
        entity.setAddTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());
        litemallActivityPicMapper.insertSelective(entity);
    }

    public void addOrUpdateQCodePic(Integer activityId, String url, LitemallAdmin admin) {
        LitemallActivityPicExample example = new LitemallActivityPicExample();
        LitemallActivityPicExample.Criteria criteria = example.createCriteria();
        criteria.andActivityIdEqualTo(activityId);
        criteria.andTypeEqualTo(LitemallActivityPic.TypeEnum.QCODE.getCode());
        criteria.andDeletedEqualTo(LitemallActivityPic.NOT_DELETED);
        LitemallActivityPic dbEntity = litemallActivityPicMapper.selectOneByExample(example);

        if (dbEntity != null) {
            dbEntity.setPicUrl(url);
            updateById(dbEntity);
        } else {
            LitemallActivityPic entity = new LitemallActivityPic();
            entity.setActivityId(activityId);
            entity.setPicUrl(url);
            entity.setType(LitemallActivityPic.TypeEnum.QCODE.getCode());
            entity.setDeleted(LitemallActivityPic.NOT_DELETED);
            addActivityPic(entity, admin);
        }
    }

    public int updateById(LitemallActivityPic activity) {
        activity.setUpdateTime(LocalDateTime.now());
        LitemallActivityPicExample example = new LitemallActivityPicExample();
        example.or().andIdEqualTo(activity.getId());
        return litemallActivityPicMapper.updateByExampleSelective(activity, example);
    }

    public void deleteById(Integer id) {
        litemallActivityPicMapper.deleteByPrimaryKey(id);

    }


    public List<LitemallActivityPic> queryActivityPic(Integer activityId, List<Byte> types) {
        LitemallActivityPicExample example = new LitemallActivityPicExample();
        LitemallActivityPicExample.Criteria criteria = example.createCriteria();
        if (activityId != null) {
            criteria.andActivityIdEqualTo(activityId);
        }
        if (!CollectionUtils.isEmpty(types)) {
            criteria.andTypeIn(types);
        }

        return litemallActivityPicMapper.selectByExample(example);
    }

}
